package tool.paoshang;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.List;

import javax.swing.JOptionPane;

import com.google.common.collect.Lists;

import tool.windows.W32API.HWND;
import tool.exception.Zy;
import tool.image.ImageTool;
import tool.keep.Keeper;
import tool.keep.Keeper.product;
import tool.keyboard.KeyBoardTool;
import tool.mouse.MouseTool;
import tool.windows.WindowsTool;

public class Luxian {
	
	public static Point gotodf = new Point(145,113);
	public static Point sdgotodf = new Point(305,155);
	public static Point gotodfsr = new Point(319,412);
	public static Point gotodfhs = new Point(254,321);
	public static Point djdfsr = new Point(186,306);
	public static Point djdfhs = new Point(441,256);
	public static Point leavedf = new Point(449,423);
	public static Point sdleavedf = new Point(415,482);
	public static Point gotodtgjcayz = new Point(183,198);
	public static Point djdtgjcayz = new Point(461,306);
	public static Point gotojnyw = new Point(592,415);
	public static Point sdgotojnyw = new Point(568,475);
	public static Point gotojyc = new Point(466,302);
	public static Point sdgotojyc = new Point(602,346);
	public static Point gotodhw = new Point(569,360);
	public static Point sdgotodhw = new Point(442,352);
	public static Point gotoddhwyf = new Point(303,380);
	public static Point gotoalsr = new Point(410,240);
	public static Point djoalsr = new Point(463,269);
	public static Point gotohgs = new Point(490,179);
	public static Point djgotohgs = new Point(512,117);
	public static Point gotobjlz = new Point(187,200);
	public static Point djhgstd = new Point(202,340);
	public static Point gotobjlzhs = new Point(187,200);
	public static Point djbjlzhs = new Point(443,219);
	public static Point gotobjlzsr = new Point(370,368);
	public static Point djbjlzsr = new Point(459,290);
	public static Point gotocsjw = new Point(420,413);
	public static Point djbjlzddg = new Point(466,328);

	public static Point djgotodyf1 = new Point(200,366);
	public static Point djgotodyf2 = new Point(440,366);

	public static boolean outBP = false;
	public static boolean iscahs = true;
	public static boolean iscshs = true;

	
    /*
    判断鼠标是否选中一站老板 dj和dyf我忘记是什么地方了
    鼠标移动方式，从左侧横向移动，每次移动20px 选中后会返回
    如果循环移动两次都未选中驿站老板会抛出异常。
    */
	public static void xzyf(HWND hwnd) {
		int i=0;
		while(true) {
			if(i++>2) {
				throw new Zy();
			}
			boolean getPlace = false;
			for(int x=djgotodyf1.x;x<=djgotodyf2.x;x+=20) {
				if(!getPlace) {
					MouseTool.MoveRelative(hwnd, new Point(x,djgotodyf1.y),3);
					getPlace = true;
				}else {
					MouseTool.MoveRelative(new Point(20,0));
				}
				if(ImageTool.ifCheckByRealMouse()) {
					return;
				}
			}
		}
	}

    /*
    判断鼠标是否选中长安传送到大唐国境的驿站
    鼠标移动方式，从左侧横向移动，每次移动20px 选中后会返回
    如果循环移动两次都未选中驿站老板会抛出异常。
    */
	public static void xzyzlb(HWND hwnd) {
		int i=0;
		while(true) {
			if(i++>2) {
				throw new Zy();
			}
			boolean getPlace = false;
			for(int x=PaoShang.cayzlb1.x;x<=PaoShang.cayzlb2.x;x+=20) {
				if(!getPlace) {
					MouseTool.MoveRelative(hwnd, new Point(x,PaoShang.cayzlb1.y),3);
					getPlace = true;
				}else {
					MouseTool.MoveRelative(new Point(20,0));
				}
				if(ImageTool.ifCheckByRealMouse()) {
					return;
				}
			}
		}
	}
    /*
    等待从商人到达长安驿站时间
    */
	public static void waitcayz() {
		sleep(14000);
	}

    /*
    打开小地图去帮派穿送人
    */
	public static void qubpcsr(HWND hwnd){
		KeyBoardTool.clickButton("tab");
		MouseTool.MoveRelative(hwnd, PaoShang.gotobpcsr,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");
	}
	

    /*
    到达帮派穿送人
    */
	private static void daodabpcsr(HWND hwnd) {

		Point point = new Point(0,0);
		do {
			sleep(1000);
			BufferedImage image = ImageTool.getScreenShoot(hwnd, Keeper.getXy());
			try {
				Point pointnow = ImageTool.imageToLocation(image);
				if(pointnow.x == point.x && pointnow.y ==point.y) {
					qubpcsr(hwnd);
				}
				point=pointnow;
			}catch (Exception e1) {
				e1.printStackTrace();
			}
		}while(Math.abs(point.x - PaoShang.daodabpcsr.x) > 2 || Math.abs(point.y- PaoShang.daodabpcsr.y)>2);
	}

    /*
    清屏 F9 + alt+H
    */
	public static void clear(Integer waitTime) {
		sleep(waitTime);
		clear();
	}

    /*
    清屏 F9 + alt+H
    */
	public static void clear() {
		KeyBoardTool.altClickButton("1");
		KeyBoardTool.clickButton("f9");
	}

    /*
    判断是否选中npc后返回
    如果没有选中则再次打开小地图，移动到npc身边，再次判断
    当连续两次都未选中那么抛出异常
    */
	protected static void xz(HWND hwnd,Point npcwz,Point npcmap,int time) {
		Point mouse = MouseTool.MoveRelative(hwnd,npcwz,2);
		if(ImageTool.ifCheck(hwnd, mouse)) return;

		mouse = MouseTool.MoveRelative(hwnd,new Point(npcwz.x-10,npcwz.y),3);
		if(ImageTool.ifCheck(hwnd, mouse)) return;

		mouse = MouseTool.MoveRelative(hwnd,new Point(npcwz.x+10,npcwz.y),3);
		if(ImageTool.ifCheck(hwnd, mouse)) return;

		mouse = MouseTool.MoveRelative(hwnd,new Point(npcwz.x-20,npcwz.y),3);
		if(ImageTool.ifCheck(hwnd, mouse)) return;

		mouse = MouseTool.MoveRelative(hwnd,new Point(npcwz.x+20,npcwz.y),3);
		if(ImageTool.ifCheck(hwnd, mouse)) return;

		mouse = MouseTool.MoveRelative(hwnd,new Point(npcwz.x-30,npcwz.y),3);
		if(ImageTool.ifCheck(hwnd, mouse)) return;

		mouse = MouseTool.MoveRelative(hwnd,new Point(npcwz.x+30,npcwz.y),3);
		if(ImageTool.ifCheck(hwnd, mouse)) return;


		if(time>1) {
			throw new Zy();
		}
		KeyBoardTool.clickButton("tab");
		MouseTool.MoveRelative(hwnd,npcmap,1);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");
		sleep(1000);
		xz(hwnd,npcwz,npcmap,++time);
	}
	
	public static void sleep(int time){
		try{
			Thread.sleep(time);
		}catch(Exception e){

		}
	}

    /*
    判断是否开始跑商或是否不跑了
    */
	public static void bangpai(HWND hwnd) {
		int time =0;
		boolean across = JOptionPane.showConfirmDialog(null, (time++>0?"重新":"")+"开始跑商？(领取银票到达长安城点击继续）")==JOptionPane.OK_OPTION?true:false;
		if(!across){
			JOptionPane.showMessageDialog(null,"已为您自动跑商：" + time + "票");
			System.exit(0);
		}
		WindowsTool.getForce(hwnd);
		sleep(1000);

		outBP=true;
	}

    /*
    使用摄妖香
    */
	public static void eatSYX(HWND hwnd) {

		KeyBoardTool.altClickButton("e");
		sleep(200);
		MouseTool.MoveRelativeOnce(hwnd, PaoShang.gotosdcsc);
		List<ProductItem> products = PaoShang.getPackageProducts(hwnd);
		boolean sheyaoxiang = false;
		for(ProductItem item:products) {

			if(item.getItem().name().equals(Keeper.product.摄妖香.name())) {

				MouseTool.MoveRelative(hwnd, new Point(PaoShang.packageStartPoint.x + item.getX()*51,PaoShang.packageStartPoint.y+item.getY()*51),10);
				MouseTool.rightClick();
				sleep(3000);
				sheyaoxiang=true;
				break;
			}
		}
		if(!sheyaoxiang) {
			throw new Zy("没有摄妖香");
		}
		PaoShang.time = System.currentTimeMillis()+20*60*1000;
		KeyBoardTool.altClickButton("e");
	}

    /*
    长安 <-> 长寿 路线
    */
	public static void cacstl2() {
		Long thresHold = 0l;
		
		HWND hwnd = PaoShang.hwnd;

		PaoShang.jcbstr = "";
		KeyBoardTool.clickButton("tab");
		sleep(300);
		PaoShang.colseRight(hwnd,Keeper.cack);
		MouseTool.MoveRelative(hwnd,PaoShang.gotocasr,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");
		if(outBP) {
			outBP = false;
			clear(47000);
		}else {
			clear(13000);
		}
		while(true) {
			try {
				xz(hwnd,PaoShang.djcasr,PaoShang.gotocasr,0);
				MouseTool.leftClick();
				if(!MouseTool.isDialogOpen(hwnd)) {
					throw new Zy();
				}
			}catch(Zy e1) {
				GameClient.validate("找不到长安商人，点击长安商人后点击ok");
			}

			long threshold = getThresHold();

			MouseTool.MoveRelative(hwnd,PaoShang.djwyjy,3);
			MouseTool.leftClick();
			sleep(2000);
			MouseTool.MoveRelativeOnce(hwnd, PaoShang.gotosdcsc);
			sleep(200);
			PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


			if(PaoShang.jobdone) {
				MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
				MouseTool.rightClick();
				throw new Zy(-99);
			}
			List<ProductItem> myProductsCasr = PaoShang.getAllMyProducts(hwnd);


			for(ProductItem item:myProductsCasr) {

				if(item.getItem() == product.木料 || item.getItem() == product.面粉 || item.getItem() == product.鹿茸 || item.getItem() == product.符) {
					setPrice(hwnd,item);
				}
			}

			List<ProductItem> casrProducts = getNpcProducts(hwnd);
			ProductItem cheapcasr = comparePrice(casrProducts);
			iscahs = false;
			ProductItem cheap = null;

			if(cheapcasr.getPrice()<1500) {
				npc2npc(hwnd,PaoShang.gotocahs,PaoShang.djcahs,"长安货商",18000);
				iscahs=true;
				List<ProductItem> myProductsCahs = Lists.newArrayList();

				for(ProductItem casrItem : myProductsCasr) {
					if(casrItem.getItem() == product.木料 || casrItem.getItem() == product.面粉 || casrItem.getItem() == product.鹿茸 || casrItem.getItem() == product.符) {
						ProductItem item = new ProductItem();
						item.setItem(casrItem.getItem());
						item.setX(casrItem.getX());
						item.setY(casrItem.getY());
						setPrice(hwnd,item);
						myProductsCahs.add(item);
					}
				}

				List<ProductItem> cahsProducts = getNpcProducts(hwnd);
				ProductItem cheapcahs = comparePrice(cahsProducts);


				if(myProductsCahs.size()>0) {
					ProductItem itemcahs = myProductsCahs.get(0);
					for(ProductItem itemcasr : myProductsCasr) {
						if(itemcahs.getItem() != itemcasr.getItem()) {
							continue;
						}
						if(itemcasr.getPrice() > (itemcahs.getPrice() + 100)) {

							if(System.currentTimeMillis()>threshold) {

								MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
								MouseTool.rightClick();
								if(iscahs) {
									KeyBoardTool.clickButton("tab");
									MouseTool.MoveRelative(hwnd,PaoShang.gotocasr,0);
									MouseTool.leftClick();
									KeyBoardTool.clickButton("tab");
									clear(18000);
								}

								continue;
							}
							npc2npc(hwnd,PaoShang.gotocasr,PaoShang.djcasr,"长安商人",18000);
							iscahs=false;

							for(ProductItem item:myProductsCasr) {

								if(item.getItem() == product.木料 || item.getItem() == product.面粉 || item.getItem() == product.鹿茸 || item.getItem() == product.符) {
									sellMyProduct(hwnd,item);
								}
							}
						}else {

							if(System.currentTimeMillis()>threshold) {

								MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
								MouseTool.rightClick();
								if(iscahs) {
									KeyBoardTool.clickButton("tab");
									MouseTool.MoveRelative(hwnd,PaoShang.gotocasr,0);
									MouseTool.leftClick();
									KeyBoardTool.clickButton("tab");
									clear(18000);
								}
								continue;
							}
							iscahs = true;

							for(ProductItem item:myProductsCasr) {

								if(item.getItem() == product.木料 || item.getItem() == product.面粉 || item.getItem() == product.鹿茸 || item.getItem() == product.符) {
									sellMyProduct(hwnd,item);
								}
							}
						}
						sleep(2000);
						PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


						if(PaoShang.jobdone) {
							MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
							MouseTool.rightClick();
							throw new Zy(-99);
						}
						break;
					}
				}

				if(cheapcasr.getItem()!=null || cheapcahs.getItem()!=null) {

					if(cheapcasr.getItem()==null) {

						cheap = cheapcahs;
					}else if(cheapcahs.getItem()==null) {

						cheap = cheapcasr;
					}else if(cheapcasr.getPrice()==cheapcahs.getPrice()){
						if(iscahs) {

							cheap = cheapcahs;
						}else {

							cheap = cheapcasr;
						}
					}else if(cheapcasr.getPrice()>cheapcahs.getPrice()) {

						cheap = cheapcasr;
					}else {

						cheap = cheapcahs;
					}
				}
				if(cheap!=null) {
					if(iscahs) {

					}else {

					}
					if(cheap==cheapcasr && iscahs) {
						npc2npc(hwnd,PaoShang.gotocasr,PaoShang.djcasr,"长安商人",18000);
						iscahs = false;
					}else if(cheap==cheapcahs && !iscahs) {
						npc2npc(hwnd,PaoShang.gotocahs,PaoShang.djcahs,"长安货商",18000);
						iscahs = true;
					}
					PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


					if(PaoShang.jobdone) {
						MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
						MouseTool.rightClick();
						throw new Zy(-99);
					}
					if(System.currentTimeMillis()>threshold) {

						MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
						MouseTool.rightClick();
						if(iscahs) {
							KeyBoardTool.clickButton("tab");
							MouseTool.MoveRelative(hwnd,PaoShang.gotocasr,0);
							MouseTool.leftClick();
							KeyBoardTool.clickButton("tab");
							clear(18000);
						}
						continue;
					}else {

						MouseTool.MoveRelative(hwnd, new Point(PaoShang.jynpcStartPoint.x+cheap.getX()*51,PaoShang.jynpcStartPoint.y+cheap.getY()*51),10);
						MouseTool.leftClick();
						MouseTool.MoveRelative(hwnd, PaoShang.djgmzdslwz,3);
						MouseTool.leftClick();
						MouseTool.MoveRelative(hwnd, PaoShang.djgmqd,3);
						MouseTool.leftClick();
						sleep(2000);
						MouseTool.leftClick();
					}
				}
			}else {

				for(ProductItem item:myProductsCasr) {

					if(item.getItem() == product.木料 || item.getItem() == product.面粉 || item.getItem() == product.鹿茸 || item.getItem() == product.符) {
						sellMyProduct(hwnd,item);
					}
				}
				sleep(2000);
				PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


				if(PaoShang.jobdone) {
					MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
					MouseTool.rightClick();
					throw new Zy(-99);
				}


				MouseTool.MoveRelative(hwnd, new Point(PaoShang.jynpcStartPoint.x+cheapcasr.getX()*51,PaoShang.jynpcStartPoint.y+cheapcasr.getY()*51),10);
				MouseTool.leftClick();
				MouseTool.MoveRelative(hwnd, PaoShang.djgmzdslwz,3);
				MouseTool.leftClick();
				MouseTool.MoveRelative(hwnd, PaoShang.djgmqd,3);
				MouseTool.leftClick();
				sleep(2000);
				MouseTool.leftClick();
				PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


				if(PaoShang.jobdone) {
					MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);
					MouseTool.rightClick();
					throw new Zy(-99);
				}
			}
			break;
		}
		MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,10);
		MouseTool.rightClick();


		KeyBoardTool.clickButton("tab");
		MouseTool.MoveRelative(hwnd,PaoShang.gotocayz,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");
		if(iscahs) {
			clear(37000);
		}else {
			clear(20000);
		}

		try {
			xzyzlb(hwnd);
			MouseTool.leftClick();

			if(!MouseTool.isDialogOpen(hwnd)) {
				throw new Zy();
			}
		}catch(Zy e1) {
			GameClient.validate("找不到驿站老板，点击驿站老板后点击ok");
		}

		MouseTool.MoveRelative(hwnd, PaoShang.djwyq,3);
		MouseTool.leftClick();


		sleep(2000);
		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.dtgjck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotodtjw,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(53000);
		MouseTool.MoveRelative(hwnd,PaoShang.gotosddtjw,2);
		MouseTool.leftClick();


		sleep(2000);
		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.dtjwck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotojwcsjw,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(147000);
		try {
			xz(hwnd,PaoShang.djjwcsjwtd,PaoShang.gotojwcsjw,0);
			MouseTool.leftClick();
			if(!MouseTool.isDialogOpen(hwnd)) {
				throw new Zy();
			}
		}catch(Zy e1) {
			GameClient.validate("找不到长寿郊外土地");
		}

		MouseTool.MoveRelative(hwnd, PaoShang.djwyq,3);
		MouseTool.leftClick();


		sleep(2000);
		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.csjwck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotocsc,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(31000);
		MouseTool.MoveRelative(hwnd,PaoShang.gotosdcsc,2);
		MouseTool.leftClick();
		sleep(2000);
		PaoShang.jcbstr = "";

		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.csck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotocssr,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(9000);
		while(true) {
			Long threshold = getThresHold();
			try {
				xz(hwnd,PaoShang.djcssr,PaoShang.gotocssr,0);
				MouseTool.leftClick();

				if(!MouseTool.isDialogOpen(hwnd)) {
					throw new Zy();
				}
			}catch(Zy e1) {
				GameClient.validate("找不到长寿商人，点击长寿商人后继续");
			}

			MouseTool.MoveRelative(hwnd,PaoShang.djwyjy,3);
			MouseTool.leftClick();
			sleep(2000);

			MouseTool.MoveRelativeOnce(hwnd, PaoShang.djgbsrdhk);

			List<ProductItem> myProductsCssr = PaoShang.getAllMyProducts(hwnd);

			for(ProductItem item:myProductsCssr) {

				if(item.getItem() == product.佛珠 || item.getItem() == product.纸扇 || item.getItem() == product.武器 || item.getItem() == product.棉布) {
					setPrice(hwnd,item);
				}
			}


			List<ProductItem> cssrProducts = getNpcProducts(hwnd);

			ProductItem cheapcssr = comparePrice(cssrProducts);

			if(cheapcssr.getPrice()<1500) {
				npc2npc(hwnd,PaoShang.gotocshs,PaoShang.djcshs,"长寿货商",11000);

				MouseTool.MoveRelativeOnce(hwnd, PaoShang.djgbsrdhk);

				List<ProductItem> myProductsCshs = Lists.newArrayList();
				for(ProductItem cssrItem : myProductsCssr) {

					if(cssrItem.getItem() == product.佛珠 || cssrItem.getItem() == product.纸扇 || cssrItem.getItem() == product.武器 || cssrItem.getItem() == product.棉布) {
						ProductItem item = new ProductItem();
						item.setItem(cssrItem.getItem());
						item.setX(cssrItem.getX());
						item.setY(cssrItem.getY());
						setPrice(hwnd,item);
						myProductsCshs.add(item);
					}
				}


				List<ProductItem> cshsProducts = getNpcProducts(hwnd);


				ProductItem cheapcshs = comparePrice(cshsProducts);


				if(myProductsCshs.size()>0) {
					ProductItem itemcshs = myProductsCshs.get(0);
					for(ProductItem itemcssr : myProductsCssr) {
						if(itemcshs.getItem() != itemcssr.getItem()) {
							continue;
						}
						if(itemcssr.getPrice() > (itemcshs.getPrice() + 100)) {

							npc2npc(hwnd,PaoShang.gotocssr,PaoShang.djcssr,"长寿商人",11000);

							if(System.currentTimeMillis()>threshold) {


								MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);

								MouseTool.rightClick();
								if(iscshs) {

									KeyBoardTool.clickButton("tab");
									MouseTool.MoveRelative(hwnd,PaoShang.gotocssr,0);
									MouseTool.leftClick();
									KeyBoardTool.clickButton("tab");
									clear(11000);
								}
								continue;
							}
							iscshs=false;

							for(ProductItem item:myProductsCshs) {

								sellMyProduct(hwnd,item);
							}
						}else {
							if(System.currentTimeMillis()>threshold) {


								MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);

								MouseTool.rightClick();
								if(iscshs) {

									KeyBoardTool.clickButton("tab");
									MouseTool.MoveRelative(hwnd,PaoShang.gotocssr,0);
									MouseTool.leftClick();
									KeyBoardTool.clickButton("tab");
									clear(11000);
								}
								continue;
							}
							iscshs = true;

							for(ProductItem item:myProductsCshs) {

								sellMyProduct(hwnd,item);
							}
						}
					}

					PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


					if(PaoShang.jobdone) {
						throw new Zy(-99);
					}
				}
				ProductItem cheap = null;
				if(cheapcssr.getItem()!=null || cheapcshs.getItem()!=null) {
					if(cheapcssr.getItem()==null) {
						cheap = cheapcshs;
					}else if(cheapcshs.getItem()==null) {
						cheap = cheapcssr;
					}else if(cheapcssr.getPrice()==cheapcshs.getItem().getPrice()){
						if(iscshs) {
							cheap = cheapcshs;
						}else {
							cheap = cheapcssr;
						}
					}else if(cheapcssr.getPrice()>cheapcshs.getPrice()) {
						cheap = cheapcssr;
					}else {
						cheap = cheapcshs;
					}
				}


				if(cheap!=null) {

					if(cheap==cheapcssr && iscshs) {
						npc2npc(hwnd,PaoShang.gotocssr,PaoShang.djcssr,"长寿商人",11000);
						iscshs = false;
					}else if(cheap==cheapcshs && !iscshs) {
						npc2npc(hwnd,PaoShang.gotocshs,PaoShang.djcshs,"长寿货商",11000);
						iscshs = true;
					}
					if(System.currentTimeMillis()>threshold) {


						MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);

						MouseTool.rightClick();
						if(iscshs) {

							KeyBoardTool.clickButton("tab");
							MouseTool.MoveRelative(hwnd,PaoShang.gotocssr,0);
							MouseTool.leftClick();
							KeyBoardTool.clickButton("tab");
							clear(11000);
						}
						continue;
					}


					MouseTool.MoveRelative(hwnd, new Point(PaoShang.jynpcStartPoint.x+cheap.getX()*51,PaoShang.jynpcStartPoint.y+cheap.getY()*51),10);
					MouseTool.leftClick();

					MouseTool.MoveRelative(hwnd, PaoShang.djgmzdslwz,3);
					MouseTool.leftClick();

					MouseTool.MoveRelative(hwnd, PaoShang.djgmqd,3);
					MouseTool.leftClick();
					sleep(2000);
					MouseTool.leftClick();
				}
				PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


				if(PaoShang.jobdone) {
					throw new Zy(-99);
				}
			}else {

				iscshs = false;

				for(ProductItem item:myProductsCssr) {

					if(item.getItem() == product.佛珠 || item.getItem() == product.纸扇 || item.getItem() == product.武器 || item.getItem() == product.棉布) {
						sellMyProduct(hwnd,item);
					}
				}

				sleep(2000);
				PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);




				MouseTool.MoveRelative(hwnd, new Point(PaoShang.jynpcStartPoint.x+cheapcssr.getX()*51,PaoShang.jynpcStartPoint.y+cheapcssr.getY()*51),10);
				MouseTool.leftClick();

				MouseTool.MoveRelative(hwnd, PaoShang.djgmzdslwz,3);
				MouseTool.leftClick();

				MouseTool.MoveRelative(hwnd, PaoShang.djgmqd,3);
				MouseTool.leftClick();
				sleep(2000);
				MouseTool.leftClick();
				PaoShang.jobdone = PaoShang.validatejobdone(hwnd,PaoShang.jynpcypjeStartPoint,PaoShang.maxMoney);


				if(PaoShang.jobdone) {
					throw new Zy(-99);
				}
			}
			break;
		}

		MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,10);

		MouseTool.rightClick();


		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.csck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotocscsjw,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		if(iscshs) {
			clear(12000);
		}else {
			clear(9000);
		}
		MouseTool.MoveRelative(hwnd,PaoShang.gotosdcscsjw,2);
		MouseTool.leftClick();


		sleep(2000);
		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.csjwck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotocsjwbjlz,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(25000);
		try {
			xz(hwnd,PaoShang.djcsjwbjlzyz,PaoShang.gotocsjwbjlz,0);
			MouseTool.leftClick();

			if(!MouseTool.isDialogOpen(hwnd)) {
				throw new Zy();
			}
		}catch(Zy e1) {
			GameClient.validate("找不到长寿郊外驿站老板，点击长寿郊外老板后继续");
		}

		MouseTool.MoveRelative(hwnd,PaoShang.djwyq,3);
		MouseTool.leftClick();


		sleep(2000);
		KeyBoardTool.clickButton("tab");
		PaoShang.colseRight(hwnd,Keeper.bjlzck);
		MouseTool.MoveRelative(hwnd,PaoShang.gotobjlzca,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(30000);
		try {
			xz(hwnd,PaoShang.djbjlzcayz,PaoShang.gotobjlzca,0);
			MouseTool.leftClick();
			if(!MouseTool.isDialogOpen(hwnd)) {
				throw new  Zy();
			}
		}catch(Zy e1) {
			GameClient.validate("找不到北俱芦洲驿站老板，点击北俱芦洲老板后继续");
		}

		MouseTool.MoveRelative(hwnd,PaoShang.djwyq,3);
		MouseTool.leftClick();
		sleep(2000);
	}
    /*
    比较商品价格
    */
	private static ProductItem comparePrice(List<ProductItem> casrProducts) {

		ProductItem cheap = new ProductItem();
		
		for(ProductItem item:casrProducts) {
			int totalprice = item.getItem().getPrice();

			if(totalprice>item.getPrice() && totalprice-item.getPrice()>cheap.getPrice()) {
				cheap.setItem(item.getItem());
				cheap.setX(item.getX());
				cheap.setY(item.getY());
				cheap.setPrice(totalprice-item.getPrice());

			}
		}
		return cheap;
	}

    /*
    卖出商品
    */
	private static void sellMyProduct(HWND hwnd, ProductItem item) {

		MouseTool.MoveRelative(hwnd, new Point(PaoShang.jymyStartPoint.x+item.getX()*51,PaoShang.jymyStartPoint.y+item.getY()*51),10);
		MouseTool.leftClick();

		MouseTool.MoveRelative(hwnd, PaoShang.djcszdslwz,3);
		MouseTool.leftClick();

		MouseTool.MoveRelative(hwnd, PaoShang.djcsqd,3);
		MouseTool.leftClick();
	}

    /*
    设置背包商品价格
    */
	private static void setPrice(HWND hwnd, ProductItem item) {

		MouseTool.MoveRelative(hwnd, new Point(PaoShang.jymyStartPoint.x+item.getX()*51,PaoShang.jymyStartPoint.y+item.getY()*51),10);
		MouseTool.leftClick();

		MouseTool.MoveRelativeOnce(hwnd, PaoShang.gotosdcsc);
		int price = PaoShang.getOwnMoney(hwnd, PaoShang.jymyzjeStartPoint);

		item.setPrice(price);
	}

    
    /*
    获取npc出售商品价格
    */
	private static List<ProductItem> getNpcProducts(HWND hwnd) {

		List<ProductItem> casrProducts = PaoShang.getAllNpcProducts(hwnd);
		int i=0;
		for(ProductItem item:casrProducts) {

			if(i++==0) {
				MouseTool.MoveRelative(hwnd, new Point(PaoShang.jynpcStartPoint.x+item.getX()*51,PaoShang.jynpcStartPoint.y+item.getY()*51),10);
			}else {
				MouseTool.MoveRelative(new Point(51,0));
			}
			MouseTool.leftClick();
			sleep(100);
			int money = PaoShang.getMoney(hwnd,PaoShang.jynpczjeStartPoint);
			item.setPrice(money);

		}
		return casrProducts;
	}

    /*
    下面两个时间用来判断是否使用摄妖香的，具体逻辑忘记了，反正应该是超过20分钟就会使用一根
    */
	private static Long getThresHold() {
		Long currentTime = System.currentTimeMillis();
		long second = currentTime%600000;
		currentTime = currentTime + 600000-second+15000;
		return currentTime;
	}
	private static Long getWaitTime() {
		Long currentTime = System.currentTimeMillis();
		long second = currentTime%600000;
		currentTime = 600000-second+15000;
		if(currentTime>600000) {
			currentTime -= 600000;
		}
		return currentTime;
	}
    
    /*
    通过小地图从一个npc走到另外一个npc的方法
    */
	private static void npc2npc(HWND hwnd,Point gotonpc, Point djnpc,String npcName, int sleepTime) {

		MouseTool.MoveRelative(hwnd, PaoShang.djgbsrdhk,5);

		MouseTool.rightClick();

		KeyBoardTool.clickButton("tab");
		MouseTool.MoveRelative(hwnd,gotonpc,0);
		MouseTool.leftClick();
		KeyBoardTool.clickButton("tab");

		clear(sleepTime);
		try {
			xz(hwnd,djnpc,gotonpc,0);
			MouseTool.leftClick();

			if(!MouseTool.isDialogOpen(hwnd)) {
				throw new Zy();
			}
		}catch(Zy e1) {
			GameClient.validate("找不到"+npcName + "，点击" + npcName + "后继续");
		}

		MouseTool.MoveRelative(hwnd,PaoShang.djwyjy,3);
		MouseTool.leftClick();
		sleep(2000);
	}

    /*
    地府 -> 傲来国 -> 长寿村 路线
    */
	public static void dfalcstl() {
		
	}
}
